package com.codefish.framework.spring.core.utils;

/**
 * 类型转换工具类
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/01 下午 08:15
 */
public abstract class ConvertUtils {


    public static <T> T convert(String str, Class<T> targetClazz) {
        Object obj = str;
        if (targetClazz == Byte.class) {
            obj = stringToByte(str);
        } else if (targetClazz == Integer.class) {
            obj = stringToInteger(str);
        } else if (targetClazz == Long.class) {
            obj = stringToLong(str);
        } else if (targetClazz == Float.class) {
            obj = stringToFloat(str);
        } else if (targetClazz == Double.class) {
            obj = stringToDouble(str);
        } else if (targetClazz == Boolean.class) {
            obj = stringToBoolean(str);
        }

        return (T) obj;
    }

    public static Byte stringToByte(String str) {
        return Byte.parseByte(str);
    }

    public static Integer stringToInteger(String str) {
        return Integer.parseInt(str);
    }

    public static Long stringToLong(String str) {
        return Long.parseLong(str);
    }

    public static Float stringToFloat(String str) {
        return Float.parseFloat(str);
    }

    public static Double stringToDouble(String str) {
        return Double.parseDouble(str);
    }

    public static Boolean stringToBoolean(String str) {
        return Boolean.parseBoolean(str);
    }


}
